const MongoClient = require('mongodb').MongoClient;
const url = "mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha";

async function main() {
  const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
  const db = client.db('taosha');


  let users = await db.collection('users')
    .find(
      {avatar_url: /dn-qiniu-avatar.qbox.me/}
    )
    .toArray();


  for (let i = 0; i < users.length; i++) {
    let user = users[i];
    let avatar_url = user.avatar_url;
    avatar_url = 'https://taosha01-1253585015.cos.ap-shanghai.myqcloud.com/typora/8651cb32bc3351842fc9c932a2e99730.png';

    try {
      let result = await db.collection('users').updateOne({_id: user._id}, {
        $set: {
          avatar_url: avatar_url
        },
      });
      console.log(result.result, i, users.length);
    } catch (e) {
      console.error(e.message);
    }
  }

  client.close();

}

main();
